package com.pl.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.LogType;
import com.pl.entity.SysUser;
import com.pl.entity.SyslogTb;
import com.pl.dao.SyslogTbDao;
import com.pl.lister.LogTypeReadLister;
import com.pl.lister.SysLogReadLister;
import com.pl.service.SyslogTbService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (SyslogTb)表服务实现类
 *
 * @author makejava
 * @since 2024-05-05 00:27:51
 */
@Service("syslogTbService")
public class SyslogTbServiceImpl implements SyslogTbService {
    @Autowired
    private SyslogTbDao syslogTbDao;

    /**
     * 通过ID查询单条数据
     *
     * @param syslogId 主键
     * @return 实例对象
     */
    @Override
    public SyslogTb queryById(Integer syslogId) {
        return this.syslogTbDao.queryById(syslogId);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<SyslogTb> queryByPage(Map<String, Object> map) {
        return this.syslogTbDao.queryAllByLimit(map);
    }

    /**
     * 新增数据
     *
     * @param syslogTb 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(SyslogTb syslogTb, HttpServletRequest request, HttpSession session) {
        //增加ip地址 创建的时候
        syslogTb.setSyslogIp(request.getRemoteHost());
        //添加的时候加入创建人
        SysUser user = (SysUser) session.getAttribute("user");
        syslogTb.setSyslogCreateBy(user.getUserName());
        //添加时间
        syslogTb.setSyslogCreateTime(new Date());

        int insert = this.syslogTbDao.insert(syslogTb);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param syslogTb 实例对象
     * @return 实例对象
     */
    @Override
    public SyslogTb update(SyslogTb syslogTb, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        syslogTb.setSyslogUpdateBy(user.getUserName());
        syslogTb.setSyslogUpdateTime(new Date());
        this.syslogTbDao.update(syslogTb);
        return this.queryById(syslogTb.getSyslogId());
    }

    /**
     * 通过主键删除数据
     *
     * @param syslogId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer syslogId) {
        return this.syslogTbDao.deleteById(syslogId) > 0;
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> list) {
        int num = syslogTbDao.deleteBatch(list);
        if (num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean finAll() {
        List<SyslogTb> list = syslogTbDao.finAll();
        if (list != null) {
            return true;
        }
        return false;
    }

    /**
     * 导入文件
     *
     * @param file
     * @return
     */
    @Override
    public String importFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SyslogTb.class, new SysLogReadLister(this)).sheet("系统日志").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";

    }

    /**
     * 批量插入
     *
     * @param list
     * @return
     */
    @Override
    public boolean inserts(List<SyslogTb> list) {
        int i = syslogTbDao.insertBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 导出文件
     * @param response
     */
    @Override
    public void exportFile(HttpServletResponse response) {
        List<SyslogTb> list = syslogTbDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(),SyslogTb.class).sheet("系统日志").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
